﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FlowerShop.Models;

namespace FlowerShop.Controllers
{
    public class ProductsController : Controller
    {
        FlowerDBEntities db = new FlowerDBEntities();
        //
        // GET: /Products/

        public ActionResult Index()
        {
            return View(DataAccessLayer.ProductsDAO.GetAllProudcts());
        }

        //
        // GET: /Products/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /Products/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Products/Create

        [HttpPost]
        public ActionResult Create(ProductsModels product)
        {
            try
            {
                // TODO: Add insert logic here
                DataAccessLayer.ProductsDAO.InsertProducts(product);
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        
        //
        // GET: /Products/Edit/5
 
        public ActionResult Edit(string id)
        {
            var pr = DataAccessLayer.ProductsDAO.SearchProducts(id);
            ProductsModels promd = Mapper.productToModel(pr);
            return View(promd);
        }

        //
        // POST: /Products/Edit/5

        [HttpPost]
        public ActionResult Edit(string id, ProductsModels product)
        {
            try
            {
                // TODO: Add update logic here
                DataAccessLayer.ProductsDAO.UpdateProducts(product);
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Products/Delete/5
 
        public ActionResult Delete(string id)
        {
            DataAccessLayer.ProductsDAO.DeleteProducts(id);
            return RedirectToAction("Index");
        }

        //
        // POST: /Products/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
                
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
